package fr.zadoka.zadokadna.Connectivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

import android.graphics.Bitmap;

public class DailyImageLoader extends UrlImageLoader {

	static final private String dailyServerUrl = "http://www.zadoka.fr/zadoka/get_daily_zadoka.php";
	
	
	public Bitmap getImage()
	{
		
		//get a connection to the server
		HttpURLConnection urlConnection = new ZadokaServerConnection().getConnection(dailyServerUrl);
		
		if (urlConnection == null)
			return null;
		
		//read the url of the image
		String dailyImageUrl = getDailyImageUrl(urlConnection);
		
		urlConnection.disconnect();
		
		//load the daily image
		return super.getImage(dailyImageUrl);
	}
	
	/// returns the url of the daily image given by connection
	public String getDailyImageUrl(HttpURLConnection connection)
	{
		if (connection == null)
			return null;
		
		String imageUrl = "";
		try {
			final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			imageUrl = bufferedReader.readLine();
			if (imageUrl == null)
				imageUrl = "";
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return imageUrl;		
	}
	
}
